package com.breaktian.demoapp.utils;

import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.breaktian.demoapp.application.DemoApplication;


/**
 * Created by breaktian on 2016/10/26.
 *
 */
public class ToastUtils {


    public static void show(String s){
        ToastProvider.get().show(s, Toast.LENGTH_SHORT);
    }
    public static void show(int resid){
        ToastProvider.get().show(resid, Toast.LENGTH_LONG);
    }
    public static void show(String s, int duration){
        ToastProvider.get().show(s, duration);
    }

    static class ToastProvider{

        private static ToastProvider instance;
        private final Handler handler;

        private ToastProvider(){
            handler = new Handler(Looper.getMainLooper());
        }

        public static ToastProvider get(){
            if(instance==null){
                synchronized (ToastProvider.class){
                    if (instance==null){
                        instance = new ToastProvider();
                    }
                }
            }
            return instance;
        }

        public void show(final String s, final int duration){

            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(DemoApplication.CONTEXT,s,duration).show();
                }
            });

        }
        public void show(final int resid,final int duration){

            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(DemoApplication.CONTEXT,resid,duration).show();
                }
            });

        }


    }




}
